{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 进位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.ceil(5.2)       #向上进位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.floor(5.2)      #向下进位"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 阶乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.factorial(5)   #阶乘"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最大公约数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.gcd(15,20)     #最大公约数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三角函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "6.123233995736766e-17\n",
      "0.9999999999999999\n",
      "1.5707963267948966\n",
      "1.5707963267948966\n",
      "0.7853981633974483\n"
     ]
    }
   ],
   "source": [
    "print(math.sin(math.pi/2))\n",
    "print(math.cos(math.pi/2))\n",
    "print(math.tan(math.pi/4))\n",
    "print(math.asin(1))\n",
    "print(math.acos(0))\n",
    "print(math.atan(1))       #三角函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 角度弧度转换\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "90.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.degrees(math.pi/2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 小孔成像案例（相似三角形）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 和差化积"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 有理数，分数，sympy的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{1}{3}$"
      ],
      "text/plain": [
       "1/3"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=Rational(1,3)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\infty$"
      ],
      "text/plain": [
       "oo"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle -\\infty$"
      ],
      "text/plain": [
       "-oo"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "-oo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\pi^{4}$"
      ],
      "text/plain": [
       "pi**4"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pi**4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.14159265358979\n",
      "3.1415926535897932385\n"
     ]
    }
   ],
   "source": [
    "print(N(pi))\n",
    "print(N(pi,20))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 代数运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x^{2} + 4 x + 3$"
      ],
      "text/plain": [
       "x**2 + 4*x + 3"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=Symbol('x')    #声明一个变量x，S大写\n",
    "x**2+4*x+3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x y z$"
      ],
      "text/plain": [
       "x*y*z"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x,y,z=symbols('x y z')  #注意，声明多个变量x，y，z，s小写，symbol加s\n",
    "x*y*z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 代数运算展开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x^{8} + 8 x^{7} y + 28 x^{6} y^{2} + 56 x^{5} y^{3} + 70 x^{4} y^{4} + 56 x^{3} y^{5} + 28 x^{2} y^{6} + 8 x y^{7} + y^{8}$"
      ],
      "text/plain": [
       "x**8 + 8*x**7*y + 28*x**6*y**2 + 56*x**5*y**3 + 70*x**4*y**4 + 56*x**3*y**5 + 28*x**2*y**6 + 8*x*y**7 + y**8"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand((x+y)**8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三角函数展开和化简"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sin{\\left(x \\right)} \\sin{\\left(y \\right)} + \\cos{\\left(x \\right)} \\cos{\\left(y \\right)}$"
      ],
      "text/plain": [
       "sin(x)*sin(y) + cos(x)*cos(y)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand(cos(x-y),trig=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle y + 1$"
      ],
      "text/plain": [
       "y + 1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify((x+x*y)/x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle - \\sqrt{2} \\cos{\\left(2 x + \\frac{\\pi}{4} \\right)} + 1$"
      ],
      "text/plain": [
       "-sqrt(2)*cos(2*x + pi/4) + 1"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(2*sin(x)*(sin(x)+cos(x)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\sum_{x=1}^{10} \\frac{1}{x^{2} + 2 x}$"
      ],
      "text/plain": [
       "Sum(1/(x**2 + 2*x), (x, 1, 10))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr = Sum(1/(x**2+2*x),(x,1,10))\n",
    "expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0.662878787878788$"
      ],
      "text/plain": [
       "0.662878787878788"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "N(expr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{175}{264}$"
      ],
      "text/plain": [
       "175/264"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr.doit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\prod_{x=1}^{10} \\frac{1}{x^{2} + 2 x}$"
      ],
      "text/plain": [
       "Product(1/(x**2 + 2*x), (x, 1, 10))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr = Product(1/(x**2+2*x),(x,1,10))\n",
    "expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{1}{869100503040000}$"
      ],
      "text/plain": [
       "1/869100503040000"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr.doit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle r \\sin{\\left(\\alpha \\right)} \\sin{\\left(\\theta \\right)} + r \\cos{\\left(\\alpha \\right)} \\cos{\\left(\\theta \\right)}$"
      ],
      "text/plain": [
       "r*sin(alpha)*sin(theta) + r*cos(alpha)*cos(theta)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r,theta,alpha=symbols('r theta alpha')\n",
    "expand(r*cos(alpha-theta),trig=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=([cos(theta),sin(theta)],[-sin(theta),cos(theta)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 2])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "point=np.array([3,2])\n",
    "point"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2*sin(theta) + 3*cos(theta), -3*sin(theta) + 2*cos(theta)],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bb= np.dot(a,point)\n",
    "bb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle -3.0$"
      ],
      "text/plain": [
       "-3.00000000000000"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(bb[0]).evalf(subs={theta:pi})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle -2.0$"
      ],
      "text/plain": [
       "-2.00000000000000"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(bb[1]).evalf(subs={theta:pi})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 极限和微积分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 1$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit(sin(x)/x,x,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle e$"
      ],
      "text/plain": [
       "E"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit((1+1/x)**x,x,oo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 0$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit(1/x,x,oo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 左极限和右极限"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\infty$"
      ],
      "text/plain": [
       "oo"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit(1/x,x,0,dir='+')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle -\\infty$"
      ],
      "text/plain": [
       "-oo"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit(1/x,x,0,dir='-')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2 x$"
      ],
      "text/plain": [
       "2*x"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff(x**2,x)   #求导的式子，对谁求导"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 2 \\cos{\\left(2 x \\right)}$"
      ],
      "text/plain": [
       "2*cos(2*x)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff(sin(2*x),x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\frac{x \\sin{\\left(\\log{\\left(x \\right)} \\right)}}{\\sqrt{x^{2} + 1}} + \\frac{\\sqrt{x^{2} + 1} \\cos{\\left(\\log{\\left(x \\right)} \\right)}}{x}$"
      ],
      "text/plain": [
       "x*sin(log(x))/sqrt(x**2 + 1) + sqrt(x**2 + 1)*cos(log(x))/x"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diff(sqrt(1+x**2)*sin(ln(x)),x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x \\log{\\left(x \\right)} - x$"
      ],
      "text/plain": [
       "x*log(x) - x"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "integrate(log(x),x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 1$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integrate(exp(-x),(x,0,+oo))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solve(x**2-3*x+2,x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 代数运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 4$"
      ],
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr=x+2\n",
    "expr.subs(x,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多元函数代数操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 62$"
      ],
      "text/plain": [
       "62"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr =x**3 + 4*x*y -z\n",
    "expr\n",
    "expr.subs([(x,3),(y,3),(z,1)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x^{2}$"
      ],
      "text/plain": [
       "x**2"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str_expr=\"x**2\"\n",
    "expr=sympify(str_expr)  #simplify的缩写\n",
    "expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 1$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr.subs(x,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 概率：色子问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入对应的库\n",
    "from sympy import *\n",
    "from sympy.stats import *\n",
    "init_printing(pretty_print=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAdIAAAAWCAYAAAB0bh0gAAAABHNCSVQICAgIfAhkiAAAB/xJREFUeJztnWmMFEUUx38LKCAYEDWghgAKBgSjEkExCgMGgxIURL7ggUbxi4koIcGghol4IKBR8SBRsaMYE40iCQqKCgIe8QAPFFHBVTwAuUERFlg/vGpmtrdnprunq7t6qV8y6aGnut7bf1dV1/GqAYvFYrFYLNoYC3wK7AIOAeem647FYrFYLImRB+qATcCbwJlhMzgLOAzsAeYC04BOnjTXALOBFcBuoB6YF9XjClRjqyPSEXjC57dLgNeBv4D96vgucEWV/no5EbgFmA/8DOxDOigrgZuBZjHbg+iamaCXy8PA+8BGRLPtwGpgKqJpnOgoY0nr5eV65O+oR8pfnGS9TgLUUtDH+9kUs62moFeSNk3QKwdMBxYr+5/5GWtRxpHBQA1wdwlnAO4BzgH2Ar8DPcvkVy3V2LoKeVDN98lzGrAVWIgIehJwHiLg21V53JAxwDPKxlLgN+RmXw08B1yu0tTHaDOqZibo5XInsApYAmwB2gAXIj3FW9X3jTHZiruMpaFXMZ2Rhmgv0FZD/lmvky67gMd8zu+N2U5T0SspmybotUx9AL4EzkfqUuCycS/SqA8tk2Yw0AN54ObQOyKtxtYiRLjmRefch9YS4Hifa46J6mgJhgAjaDzy7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yle \\left\\{ 1 : \\frac{1}{6}, \\  2 : \\frac{1}{6}, \\  3 : \\frac{1}{6}, \\  4 : \\frac{1}{6}, \\  5 : \\frac{1}{6}, \\  6 : \\frac{1}{6}\\right\\}$"
      ],
      "text/plain": [
       "{1: 1/6, 2: 1/6, 3: 1/6, 4: 1/6, 5: 1/6, 6: 1/6}"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X= Die('X', 6)  #创建一个6个面的筛子\n",
    "density(X).dict #查看某个面出现的概率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA0AAAASCAYAAACAa1QyAAAABHNCSVQICAgIfAhkiAAAAMJJREFUKJHF0j9LQlEYB+DH6BNYhJ/HxcmpD9CgQxCImxAIbi5u7eLSJji5NzQGDRItDtHQ3J0aSnS4LxJytBMN/eBw4XCe+77nD3/MCdqYYYkPFLhHC0cpdIk13nCLIcZ4j/kpKruojmbijzW8Bjz/TevXgW7s6zORz/h+5VY5xiIqNXLRKMA8F3QCPKOaA64CPClP8Md0AyxwlgN6AR5xmgP6AR4c2MP3Z3GBCVbKSywS619izTaDqHJo3OW0+8/ZALBUMPfsVQVVAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$\\displaystyle 2$"
      ],
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample(X) #随机丢一次筛子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAACEAAAAUCAYAAAADU1RxAAAABHNCSVQICAgIfAhkiAAAAX5JREFUSInt1bFrFEEYxuFHI2J3EMWztfBPSCnkbCxEiWhloUUkCIKICIIQsEoaK6/3UsRK0Uoi2ARiGVAQuSbIkYCVRUwQkSTEYj6b5O72Oy5FEF8Ydr7dd3777s7sDodQ19HEEjawi/nEuDp28Czqk7iNN1jBL/zAB0ziaD/Yp7jxJtoDhJgKbyPqO1F/wwvM4jnW4/wrHOkFa+BcGMYHCLGA7xiJ+gIu2//EZ7Aa3GsJbjpEDb/RykDxOLjNvyf6zk1Sl3Acr5P+rThuH2SIq/iJ9wnvMdyM/rsMfFz1dJxQFvHLDBBPg/k26U+FuBKeGwnevfC2MXqQIVrKoqxVsO4G64vyhaRVFWJE+SwXKjj3g/MZpwcJkAnRiOtTfRiPwvMRpwYNkAnRVH7V9R7Xp2P8soo1sPfXORGNMncX8VXZSyiv/2H019DB+S7cW5iLkE1l39irTnj26Umk79U64RuL+kE3SIKzi8UeY9OaCdDZYUHDqK3suP/17+kPbkFvvLHg50MAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\frac{1}{2}$"
      ],
      "text/plain": [
       "1/2"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P(X > 3) #丢出筛子大于3的概率 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 硬币问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKMAAAAWCAYAAACsayxGAAAABHNCSVQICAgIfAhkiAAABMVJREFUaIHtmmuoFkUYx396TlkpaRfSsCC7m8kxL5Vfao8VFGJmJoFSFqcyCKoPRYGFUpgSSRepDMLOB+tDgRYlCkEax0vmLYmwqA8WQSdSyNKOZZ3jh+fZ3m3P7OzM7uyb1v5g2Zcz/5l5zv+dmZ159oWamuOE2cAnwAHgL2DcvxtOzXHKQuAI0A28C1zs28BlQC/wK7ACeBoYYdB9CPQB11raWq6ae32DyOA2YBnQBfyiba90rDscmVgvAWcA9wCrgW+AHmTibQQ6gIGB4k3zExKz63V/ib5CeQXF/YqAJcA67f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nHO+mfsS/LOMSYJOfrBfAtDYMnAecge9w0o/TeguyBTHxMf3PHIk+GsqmfZuQXQ3kFxf0y4fVGKM4HzfCpdAzyDPJ/jMoTOjIMWRmeDdTesURor0y8p31kbQOMzNRKHyAvtav6OVXV7AE+C9jeNOQtQrNSP80ktFdJBgFXIj85O4DneDoZObYn36LUP66tKcJC/jmOFphEtj1jD/LueQqNA0t30BBr/i9sQA4s+4EtVLf61tTU1PzHOApgXnvGYfBhkwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle \\left\\{ H : \\frac{1}{2}, \\  T : \\frac{1}{2}\\right\\}$"
      ],
      "text/plain": [
       "{H: 1/2, T: 1/2}"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = Coin('C')  # A coin\n",
    "density(C).dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKEAAAAWCAYAAAConvx7AAAABHNCSVQICAgIfAhkiAAAA81JREFUaIHtmkloFEEUhr8YVxCTqBhFRD0obgcPKnjruIAQ4hZPORhFiYKgIoJeRFE0IgqiF086h+Si4oKKAUEDCSpKiCCSgx5GEYmgYBSN4hIP9dpJ2p7u6uquzCT0D0NPT7/3/n/+qa6pqi5IkaLIUQc8BnqA38DiwspJMcRwBPgJdAM3gLlRCywA/gBfgIvAMWCqJ2YTcB5oAz4DfUCTqeIQxOGqRN1E5+R8ErAduA68AnpRN1o7sA0YkZjqHArBaepZUn45wEmgRbifRP0CuyRxd0DMM4n5AnRhtxHG4WqQ+Co53ynn74BmoBF1o32Sz68CJUkJLyCnqWc2/OpAdWrjo3yBQ0KwOiCmCpgjAhzsNsI4XHeBD0CpnK8Aavj/Dp4KvJHatfHk/odCcJp6ZsOvZomb4b0Q9BfgCvgZEPMAeCnFbcOUqwxl4i3UXwzAfTn/44ntBi7Ie8dIZX4UgtPEM1t+ue2o1HvBxjgkKjIok7ZYql8NjAauaca7Zv2yIyc2Z4Zh5ldQIyyXY69p8SLBBuArcE8jdiSwWd63WFNUeM4g2PLruxzLdIWUkBtIemfE+eBgNiacBsyLIi4C11jUoPyKZt3TUvdOBC1xEZXTxC/Q88ymXwcldk9Y4BrgDPBUEi4Ehw+Ag92JiQnXWomr06i5W2K7gIlxxEXAYHI6hHtm06/pQBb1930ZOA7M8gs8K4X7gOfAQo3iLhyKrxFeAn4Q3mu4y1Ev0O/542KwOR3CPbPt1w7gG7k25uQLnACsQ40F3+Izm8kDh+JqhKWoZYa7IbX2krvppiQhTgOF4HQI9sy2X9WS147q3EbpJDVJ0iJNEofiaoRVEtMQEHNAYjqByUmJC0EhOCHcM9t+nZHcVd4LQbPj13K0PVYxHWiHYSNqYnUzz/VDqEdKHcBKVC8QhgzxlkdMOL0YSn71R4Ucs94LIwOS3PWfoIa6Xl6QGxssR/1YoITuDxHXCNQDW/vlJcG1HngIvPepVQ8cRS3GtuH/aDLro8f1wmRNzJTTC12/IJpnNvzqDyPvDhMyeETtkugLeGU1eDLo9S5RuJbKZ/sMa/UBrT55naiNABU+18JgyulFBv3eOIwzK3G2/PLTPVND9z+46zoboiQVCU6gtM9OsGY5qic4lWDNYoENv7y4KRyVUZJqJek26qFzMTzi00UXagdJkqhBrfoP1hLOYMKGXy7GAMtQW796iNiOxqGm4f273HRTa4ooOMLA9nPYLyhoYtILLEHtqJiP2gfWnajEFMMdraiJyEfgEfZ62xQpUqQY4vgLyCubLvi/6FwAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle \\left\\{ 1 : \\frac{1}{2}, \\  2 : \\frac{1}{2}\\right\\}$"
      ],
      "text/plain": [
       "{1: 1/2, 2: 1/2}"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X= Die('X', 2)  #创建一个2个面的筛子\n",
    "density(X).dict #查看某个面出现的概率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 正态分布"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAANMAAAASCAYAAADBs+vIAAAABHNCSVQICAgIfAhkiAAABz9JREFUaIHt2nvsXFURB/AP9VdtqyWI1TYqAhYSsE1QjCga6FIeUrVa8PEHETCKGCFS8IFFMKxPBAmKoGKDDwzRqEghVtHWBixUCaJUqKKC9Kcl0NRW+y6tLT//mHOz27v37uPuFhOz32RzN+fMnJnzmDlz5hyGGGKIfYaX4lt4Ajsxii/j+T228w5ch7uxGWO4uQ39aKIp+q1tw/dmLMHj2IHH8CMc20G/4/BjPCn6+WRq500D0KtXnhfgHCzCo6kfm3AP3odxA5JzJZZhTZLxLzyAy5MORdgP78W92ILtiecCPKuEp4qcXtdLHmdq9P2cEppRvY3Xe9rQZ789GfFIjnk6fo0X4Xb8GcdgPk7FG7Chy85dhqOwVSz0I7rg2SQMN4+tJfRX4uKk021Yj8PwNrwdZymekMvwmUS/WBjSFLwKNfysT7165Xknvp70uBP/wFScjhsxJ9GM9SnnIvweS7EOz8XrUMe56f+aHM9NYqGuww+wDSfhWhxfolcVOVXWS4aDhCFuxfM60PYyXivxqZJ2jsNs3FEm6BdiYD6UK78mld/QQdFmnIDDhWer6W5nGu2h/WnCK6wVxp+XPSZ2qTyyyV+KyQX14/vUqwrPbMzVugNNE4Y1JpxDv3ImlJR/Lsn4Wq58nsY4TmkqHy920THhvfuVQ+/rJcN++CX+hi/qvDONdtFmN/hNkvXWosqXp8rVWid1srDcbcLL9IqawRvTa1Obt5fUbxZhSTPGiYWxDS/sUk6velXlKcMnRD+v24dyjtJwMM34bio/v4BnZqr73QDk5FHTvTHNx9Nil6x7Zowp6/vjmkLd5jBvdvouSco1YwtW4BSxRS8bgEJFeA7ejZeJBf8glmuKS5vwCHaJMHSKCNkyHC8cwG05ntfjUNyCf4vz1kw8hfuE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qN+/oOw1Iumzxd5hdD9ymlFTLZKpKw/zqs5Lhuy50k/KCPLPic4Tz4m+IsK5h8Xl6AnisHlpjn5ZUuZQrd58XvrRGNxj8Z30f71InRKvEhaI3XG1mKTp4h5ogjgEX51r/xZx831S0nORMKwj8RZxM75Aaxz84dSnS8V91H2pD6cJb/l+jTuTKnpV4TlbIzt1t0g+5DGqMXZV5Jwqwtjl4rXABpEYmCUMYm3qex5LhVNalWTMEO8Xd4qwOe/Nq8rpZb1UQZV5aca56buwF6EH4dvindgu/F3EikVWPSqs9ZCCurr28exoE+0sfF+8BdwowoZ/iok8SxhGEcaLM8a94mC8W9w5LdY+w3Sg2G1Xpz5uEC8p8vclVfSqwlPXOf6/q085M/FVEUKuF2O1SZwf68p3w4+JVw4bhQGtFhHKISX0VeXUdb9eypC1UbQzVV1jhIPuKvEwxBBDDDHEEEMMMcT/Hf4L8CITUDfS514AAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle 0.158655253931457$"
      ],
      "text/plain": [
       "0.158655253931457"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z = Normal('Z', 0, 1)  # 正态分布 0为期望,1是标准差, 这是个标准的正态分布\n",
    "P(Z > 1).evalf()  # 概率 Z > 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 概率叠加，贝叶斯分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "#回头自己看一下"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
